<?php
/**
 * @Author: hpp
 * @Date:   2018-12-04 17:57:46
 * @Last Modified by:   hpp
 * @Last Modified time: 2018-12-05 18:07:03
 */
namespace app\admin\controller;

use think\Request;
use app\admin\model\Role as RoleModel;
use app\admin\model\Permission;
use app\admin\model\PermissionRole;

class Role extends BaseController
{
    /**
     * 显示资源列表
     */
    public function index()
    {
        $list = RoleModel::order('id','desc')->paginate(RoleModel::PAGESIZE);

        return $this->fetch('role/index',compact('list'));
    }

    /**
     * 保存新建的资源
     */
    public function save(Request $request)
    {
        $data = $request->param();
        $res = $this->validate($data,'app\admin\validate\Role');
        if($res !== true) return $this->error($res);
        $insertRes = RoleModel::create($data);
        if(!$insertRes){
            return $this->error('添加失败');
        }
        return $this->success('添加成功');
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $data = RoleModel::field('id,name,desc')->find(intval($id));
        return json(['code' =>1, 'data' => $data]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->param();
        if(is_numeric($data['name'])){
            return $this->error('名称不能是数字');
        }
        if(!$data['name']){
            return $this->error('名称不能为空');
        }
        unset($data['id']);
        $updateRes = RoleModel::where('id',$id)->update($data);
        if($updateRes){
            return $this->success('修改成功');
        }else{
            return $this->error('修改失败');
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $res = RoleModel::destroy($id);
        if($res){
            return $this->success('删除成功');
        }else{
            return $this->error('删除失败');
        }
    }

    public function deletes(){
        $ids = input('get.ids');
        if(empty($ids) || !is_array($ids)){
            return $this->error('请选择要删除的数据');
        }
        $res = RoleModel::destroy($ids);
        if($res){
            return json(['code' => 1,'msg' => '删除成功','data' => $ids]);
        }else{
            return $this->error('删除失败');
        }
    }

    /*
     * 赋予权限
     */
    public function permission($role_id){
        $class_ele = ['warning-element','success-element','info-element','danger-element'];
        // 当前角色拥有的权限
        $permission = RoleModel::permission($role_id);
        $permissions = Permission::getTreeView();

        for($i=0; $i<count($permissions); $i++){
            $rand = mt_rand(0,100);
            $rand = $rand%4;
            $rand_num[] = $rand;
        }
        foreach($permissions as $k=>$v){
            $permissions[$k]['class'] = $class_ele[$rand_num[$k]];
        }
//        dump($permissions);die;
        return $this->fetch('role/permission',compact('permission','permissions','role_id'));
    }

    public function store(Request $request){
        $ids = $request->param();
        parse_str($ids['ids'],$idArr);
        $idStr = join(',',isset($idArr['ids']) ? $idArr['ids'] : []);
        $data = PermissionRole::where('role_id',$ids['role_id'])->find();
        if($data){
            $data->permission_id = $idStr;
            $res = $data->force()->save();
        }else{
            $res = PermissionRole::create(['role_id' => $ids['role_id'],'permission_id' => $idStr]);
        }
        if($res){
            return $this->success('操作成功');
        }else{
            return $this->error('操作失败');
        }
    }
}
